package com.google.code.sysarch.v4.sound;
import java.io.IOException;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.openal.AL;
import org.lwjgl.openal.AL10;
import org.lwjgl.openal.ALC10;
import org.lwjgl.util.WaveData;


public class SoundTest {
	
	IntBuffer buffer = BufferUtils.createIntBuffer(1);
 	IntBuffer source = BufferUtils.createIntBuffer(1);
  	FloatBuffer sourcePos = BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f });
  	FloatBuffer sourceVel = BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f });
  	FloatBuffer listenerPos = BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f });
  	FloatBuffer listenerVel = BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f });
  	FloatBuffer listenerOri = BufferUtils.createFloatBuffer(6).put(new float[] { 0.0f, 0.0f, -1.0f,  0.0f, 1.0f, 0.0f });  
  
  
  	public SoundTest() {
    	sourcePos.flip();
    	sourceVel.flip();
    	listenerPos.flip();
    	listenerVel.flip();
    	listenerOri.flip();
  	}
  
  
	int loadALData() {
		AL10.alGenBuffers(buffer);
		if (AL10.alGetError() != AL10.AL_NO_ERROR) return AL10.AL_FALSE;
		WaveData waveFile = WaveData.create("res/sound/explosive_chase_soundpack_v2.0/30mmc3.wav");
		AL10.alBufferData(buffer.get(0), waveFile.format, waveFile.data, waveFile.samplerate);
		waveFile.dispose();
		AL10.alGenSources(source);
		if (AL10.alGetError() != AL10.AL_NO_ERROR) return AL10.AL_FALSE;
		AL10.alSourcei(source.get(0), AL10.AL_BUFFER, buffer.get(0));
		AL10.alSourcef(source.get(0), AL10.AL_PITCH, 1.0f);
		AL10.alSourcef(source.get(0), AL10.AL_GAIN, 1.0f);
		AL10.alSource (source.get(0), AL10.AL_POSITION, sourcePos);
		AL10.alSource (source.get(0), AL10.AL_VELOCITY, sourceVel);
		if (AL10.alGetError() == AL10.AL_NO_ERROR) return AL10.AL_TRUE;
		return AL10.AL_FALSE;
	}
  
	
	void setListenerValues() {
		AL10.alListener(AL10.AL_POSITION, listenerPos);
		AL10.alListener(AL10.AL_VELOCITY, listenerVel);
		AL10.alListener(AL10.AL_ORIENTATION, listenerOri);
	}
	
	
	void killALData() {
		AL10.alDeleteSources(source);
		AL10.alDeleteBuffers(buffer);
	}
	
	
	public void execute() {
		try {
			AL.create(null, 15, 22050, true);
		} catch (LWJGLException le) {
			le.printStackTrace();
			return;
		}
		AL10.alGetError();
		if(loadALData() == AL10.AL_FALSE) {
			System.out.println("Fehler beim Laden der Daten.");
			return;
		}
		setListenerValues();
		System.out.print("p) Wiedergabe\n");
		System.out.print("s) Stop\n");
		System.out.print("h) Pause\n");
		System.out.print("q) Beenden\n\n");    
    
		char c = ' ';
		while (c != 'q') {
			try {
				c = (char) System.in.read();
			} catch (IOException ioe) {
				c = 'q';
			}
			
			switch(c) {
				case 'p':
					AL10.alSourcePlay(source.get(0));
					break;
				case 's':
					AL10.alSourceStop(source.get(0));
					break;
				case 'h':
					AL10.alSourcePause(source.get(0));
					break;
				case '1':
					System.out.println("Suspend");
					ALC10.alcSuspendContext(AL.getContext());
					break;
				case '2':
					ALC10.alcProcessContext(AL.getContext());
					break;
			}
		}
		killALData();
	}
	
	
	public static void main(String[] args) {
		new SoundTest().execute();    
	}
	
	
}